// pages/cityChoose/cityChoose.js
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    locationCity: "",
    hotCity: ["北京","上海","广州","杭州","深圳","成都","武汉","南京","重庆","天津"],
    allCity: []
  },
  

  // 选择定位城市
  chooseCity: function(){
    app.globalData.chooseCity = this.data.locationCity;
    wx.navigateBack({
      url: "../launch/launch"
    })
  },

  // 选择热门城市
  chooseHotCity: function(e){
    var index = e.currentTarget.dataset.index;
    app.globalData.chooseCity = this.data.hotCity[index];
    wx.navigateBack({
      url: "../launch/launch"
    })
  },

  // 在所有城市中选择
  allCityChoose: function(e){
    var index = e.currentTarget.dataset.index;
    app.globalData.chooseCity = this.data.allCity[index].name;
    wx.navigateBack({
      url:"../index/index"
    })
  },

  // 获取城市
  getCity: function(){
    wx.showLoading({
      title: '加载中',
    })
    wx.request({
      url: app.globalData.url + '/ad/city/list', 
      method: "POST",
      data:{
        access_token: app.globalData.accessToken,
      },
      success: res =>{
        var list = res.data.data.city_list;
        this.setData({
          allCity: list
        })
        wx.hideLoading();
      },
      fail: res =>{
        console.log(res)
      }
    })
  },
  

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (e) {
    this.setData({
      locationCity: app.globalData.locationCity
    })
    if (app.globalData.accessToken != ""){
      this.getCity();
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  
  }
})